// MouseMage, a rodent behavioral data collection and analysis program for Android devices.
// Copyright (C) 2012 Joseph Van Swearingen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.zythran.mousemage;

import android.util.Log;

public class MouseIteration {
	public int timepoint ;
	public int bin ;
	public int animal ;
	
	public boolean asleep_marked ;
	public boolean inactive_marked ;	

	public int move_level ;
	public boolean exploring ;
	public boolean movement_pattern ;
	public boolean move_fast ;

	public int pivot_level ;

	public int groom_level ;
	public boolean groom_intense ;	

	public int rear_level ;

	public boolean limbs_splayed ;
	public boolean injury ;
	public boolean seizures ;
	public boolean laying_on_side ;

	public int sniff_level ;	
	public boolean head_up ;
	public boolean head_down ;
	public boolean head_fixed ;
	public boolean sniff_intense ;

	public boolean head_bob ;
	public boolean twitching ;
	public boolean eating_bedding ;
	public boolean flat_position ;
	public boolean with_licking ; // rear with licking
	public boolean mouth_mvmt ; // mouth/tongue movement
	public boolean erect_tail ;

	public int final_result ;

	public String other_dysk_text ;
	public String novel_behavior_text ;
	
	public int REAR_LEVEL_MAINTAINED = 3 ;
	public int REAR_LEVEL_CONTINUOUS = 2 ;
	public int REAR_LEVEL_INTERMITTENT = 1 ;
	
	public int MOVE_LEVEL_INTERMITTENT = 1 ;
	public int MOVE_LEVEL_CONTINUOUS = 2 ;
	public int MOVE_LEVEL_MAINTAINED = 3 ;
	
	public int GROOM_LEVEL_INTERMITTENT = 1 ;
	public int GROOM_LEVEL_CONTINUOUS = 2 ;
	public int GROOM_LEVEL_MAINTAINED = 3 ;
	
	public int SNIFF_LEVEL_ONCE = 1 ;	
	public int SNIFF_LEVEL_INTERMITTENT = 2 ;
	public int SNIFF_LEVEL_CONTINUOUS = 3 ;
	public int SNIFF_LEVEL_MAINTAINED = 4 ;	
	
	public int PIVOT_LEVEL_ONCE = 1 ;	
	public int PIVOT_LEVEL_INTERMITTENT = 2 ;
	public int PIVOT_LEVEL_CONTINUOUS = 3 ;
	public int PIVOT_LEVEL_MAINTAINED = 4 ;	
	
	public MouseIteration() {
	    asleep_marked = false ;
	    inactive_marked = false ;
	    sniff_level = 0 ;
		sniff_intense = false ;	    
	    rear_level = 0 ;
	    groom_level = 0 ;
	 	groom_intense = false ;	    
	    pivot_level = 0 ;
	    move_level = 0 ;
	    exploring = false ;
	    movement_pattern = false ;
	    move_fast = false ;	    
	    limbs_splayed = false ;
	    injury = false ;
	    head_up = false;
	    head_down = false;
	    head_fixed = false;
	    head_bob = false;
	    eating_bedding = false;
	    final_result = 0;
	    other_dysk_text = new String() ;
	    novel_behavior_text = new String() ;
	    with_licking = false ; 
		flat_position = false ;
		mouth_mvmt = false ;
		erect_tail = false ;
		seizures = false ;
		laying_on_side = false ;
	}
	
	public String toStr(boolean val) {
		  String ret = val ? "1" : "0" ; 
		  return ret ;
	}
	public String toString() {
		String final_result_s = (final_result == 0) ? "?" : "" + final_result ;
		  String foo = "" + timepoint
			 + ", " + animal
			 + ", " + bin
			 + ", " + final_result_s
			 + ", " + toStr(asleep_marked)
			 + ", " + toStr(inactive_marked)
			 + ", " + move_level
			 + ", " + toStr(exploring)
			 + ", " + toStr(movement_pattern)
			 + ", " + toStr(move_fast)
			 + ", " + pivot_level
			 + ", " + groom_level
			 + ", " + toStr(groom_intense)
			 + ", " + rear_level
			 + ", " + toStr(limbs_splayed)
			 + ", " + toStr(injury)
			 + ", " + toStr(seizures)
			 + ", " + toStr(laying_on_side)
			 + ", " + sniff_level
			 + ", " + toStr(head_up)
			 + ", " + toStr(head_down)
			 + ", " + toStr(head_fixed)
			 + ", " + toStr(sniff_intense)
			 + ", " + toStr(head_bob)
			 + ", " + toStr(twitching)
			 + ", " + toStr(eating_bedding)
			 + ", " + toStr(flat_position)
			 + ", " + toStr(with_licking)
			 + ", " + toStr(mouth_mvmt)
			 + ", " + toStr(erect_tail)
			 + ", " + other_dysk_text
			 + ", " + novel_behavior_text 
			 ;
		  return foo ;
	}

	public void setState(String state) {
		String end_of_string ;
		
		if (state == "") {
			Log.i("nostate", "state was not valid") ;
			return ;
		} else {
			Log.i("state", "Trying to set this state:") ;
			Log.i("state", state) ;
			Log.i("state", state.substring(2, 3)) ;
			
		}
		
		asleep_marked = (state.charAt(0)== '1') ? true : false ;
		inactive_marked = (state.charAt(1)== '1') ? true : false ;
		sniff_level = Integer.parseInt(state.substring(2, 3)) ;
		rear_level = Integer.parseInt(state.substring(3, 4)) ;
		groom_level = Integer.parseInt(state.substring(4, 5)) ;
		pivot_level = Integer.parseInt(state.substring(5, 6)) ;
		move_level = Integer.parseInt(state.substring(6, 7)) ;
		exploring = (state.charAt(7)== '1') ? true : false ;
		movement_pattern = (state.charAt(8)== '1') ? true : false ;
		limbs_splayed = (state.charAt(9)== '1') ? true : false ;
		injury = (state.charAt(10)== '1') ? true : false ;
		head_up = (state.charAt(11)== '1') ? true : false ;
		head_down = (state.charAt(12)== '1') ? true : false ;
		head_fixed = (state.charAt(13)== '1') ? true : false ;
		head_bob = (state.charAt(14)== '1') ? true : false ;
		twitching = (state.charAt(15)== '1') ? true : false ;
		eating_bedding = (state.charAt(16)== '1') ? true : false ;
	    move_fast = (state.charAt(17)== '1') ? true : false ;
	 	groom_intense = (state.charAt(18)== '1') ? true : false ;
		sniff_intense = (state.charAt(19)== '1') ? true : false ;
		with_licking = (state.charAt(20)== '1') ? true : false ;
		flat_position = (state.charAt(21)== '1') ? true : false ;
		mouth_mvmt = (state.charAt(22)== '1') ? true : false ;
		erect_tail = (state.charAt(23)== '1') ? true : false ;
		seizures = (state.charAt(24)== '1') ? true : false ;
		laying_on_side = (state.charAt(25)== '1') ? true : false ;
		final_result = Integer.parseInt(state.substring(26, 27)) ;
		  // End of string has starting @ for each to guarantee token
	    end_of_string = state.substring(27) ;
	    
	    String eos[] = end_of_string.split(":::") ;
	    other_dysk_text = eos[0].substring(1) ;
	    novel_behavior_text = eos[1].substring(1) ;	
	}
	
	public String getState() {
		String foo = "" 
			+ toStr(asleep_marked)
			+ toStr(inactive_marked)
			+ sniff_level
			+ rear_level
			+ groom_level
			+ pivot_level
			+ move_level
			+ toStr(exploring)
			+ toStr(movement_pattern)
			+ toStr(limbs_splayed)
			+ toStr(injury)
			+ toStr(head_up)
			+ toStr(head_down)
			+ toStr(head_fixed)
			+ toStr(head_bob)
			+ toStr(twitching)
			+ toStr(eating_bedding)
			+ toStr(move_fast)
			+ toStr(groom_intense)
			+ toStr(sniff_intense)
			+ toStr(with_licking)
			+ toStr(flat_position)
			+ toStr(mouth_mvmt)
			+ toStr(erect_tail)
			+ toStr(seizures)
			+ toStr(laying_on_side)
			+ final_result + "@"
			+ other_dysk_text + ":::@"
			+ novel_behavior_text ;
		
		Log.i("MI", "On getState, result is:") ;
		Log.i("MI", foo) ;
		return foo ;

	}

}
